<?php
declare (strict_types=1);

namespace app\admin\validate;

use think\Validate;

class Flag extends Validate
{
    protected $rule = [
        'name'   => 'require',
        'code'   => 'require|checkExistCode',
        'status' => ['require', 'in' => '1,2'],
    ];

    protected $message = [
        'name.require' => '请输入名称',
        'status'       => '请选择正确的状态',
    ];

    protected $scene = [
        'add'  => ['name', 'code', 'status'],
        'edit' => ['name', 'status']
    ];

    public function checkExistCode($value, $rule, $data = [])
    {
        $flagModel = new \app\common\model\Flag([], $data['website_code']);
        $flagObj   = $flagModel->findFlagByCode($value);
        if (!empty($flagObj)) {
            $flag = $flagObj->toArray();
            if (isset($data['id']) && $flag['id'] != $data['id']) {
                return '站点标识已存在';
            }
        }

        return true;
    }
}
